/*
 * @(#)ImgTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import javax.servlet.jsp.JspException;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;

/**
  ImgTag.java

  <p>This class is to extend the ImgTag provided by Apache Struts.<p>
  <p>Modification in "struts-html.tld"<br>
  <pre>
     [FROM]
     &lt;tagclass&gt;org.apache.struts.taglib.html.ImgTag&lt;/tagclass&gt;
     [TO]
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.ImgTag&lt;/tagclass&gt;

     [ADDED]
     &lt;attribute&gt;
     &lt;name&gt;common&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;customize&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;pathOnly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
  </pre>
  </p>

  <p>USAGE:</p>
  <p>The following example shows how to show a language independent image which
     stored in /common/ folder.</p>
  <p><code>&lt;html:img src='IconNext.gif' <strong>common='true'</strong>/&gt;</code></p>
  <p>The following example shows how to show a language dependent image which
     stored in /en_US/, /zh_HK/ or /zh_CN/ folders.</p>
  <p><code>&lt;html:img src='ButtonGo.gif'/&gt;</code></p>
  <p>The following example shows how to show a customized image which
     stored in /customize/ folder.</p>
  <p><code>&lt;html:img src='SomeClipart.gif' <strong>customize='true'</strong>/&gt;</code></p>

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   04/07/2003
    @version         $Revision: 1.1.2.1 $
*/

public class ImgOldImplTag extends org.apache.struts.taglib.html.ImgTag {

  private static final long serialVersionUID = 1L;
  private boolean common = false;
  private boolean pathOnly = false;
  private boolean customize = false;
  private String altLabel = null;
  
  public ImgOldImplTag() {
    super();
    this.border = "0";
    this.align = "absmiddle";
  }

  public boolean getCommon() {
    return(this.common);
  }

  public void setCommon(boolean common) {
    this.common = common;
  }

  public boolean getPathOnly() {
    return(this.pathOnly);    
  }

  public void setPathOnly(boolean pathOnly) {
    this.pathOnly = pathOnly;
  }

  public boolean getCustomize() {
    return(this.customize);
  }

  public void setCustomize(boolean customize) {
    this.customize = customize;
  }

  public int doStartTag() throws JspException {
    this.border = "0";
    this.align = "absmiddle";   
    this.init();
    if (this.getPathOnly()) {
      try {
        pageContext.getOut().print(this.src());
        return(SKIP_BODY);
      } catch (Exception e) {
        throw new JspException(e);
      }
    } else {
      return(super.doStartTag());
    }
  }

  public int doEndTag() throws JspException {
    if (this.getPathOnly()) {
      return(this.EVAL_PAGE);
    } else {
      return(super.doEndTag());
    }
  }

  protected java.lang.String src() throws javax.servlet.jsp.JspException {
    String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    String preference = SystemParameterFactory.getSystemParameter(SystemParameterConstant.PREFERENCE);
    Object tmpLocale = pageContext.getSession().getAttribute(org.apache.struts.Globals.LOCALE_KEY);
    String localeStr = tmpLocale == null ? SystemParameterFactory.getSystemParameter(SystemParameterConstant.LOCALE) : tmpLocale.toString();

    if ((sessionContainer!=null) && (sessionContainer.getUserRecord()!=null)) {
      if (sessionContainer.getUserRecord().getPreference()!=null) {
        preference = sessionContainer.getUserRecord().getPreference().toString();
      }
    }

    if (this.getCommon()) {
      return(contextPath + "/theme/pref" + preference + "/img/common/" + this.src);
    } else if (this.getCustomize()) {
      return(contextPath + "/theme/pref" + preference + "/img/customize/" + this.src);
    } else {
      return(contextPath + "/theme/pref" + preference + "/img/lang/" + localeStr + "/" + this.src);
    }
  }

  /**
   * init the tag
   * tc add 2007/04/26
   */
  private void init(){
    if(!Utility.isEmpty(this.getAltLabel())){
      SessionContainer sessionContainer = (SessionContainer)pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
      String altStr=MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(),this.altLabel);
      this.setAlt(altStr);
    }
  }
  
  public void release() {
    super.release();
    common = false;
    pathOnly = false;
    customize = false;
    altLabel = null;
  }

  public String getAltLabel() {
    return altLabel;
  }

  public void setAltLabel(String altLabel) {
    this.altLabel = altLabel;
  }

}